import os
import django
import json

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'application.settings')
django.setup()


from pathlib import Path
from station.views import station,guowangbuliang,pbom
from station.utils.excelTodb import ExcelToDb
from station.views.weldspot import handian, hanqiang, cailiao

def import_excel_data(path, model):
    # 设置读取的excel文件路径
    cur_path = Path(__file__).resolve().parent
    #station
    excel_path = Path.joinpath(cur_path,path)
    abs_path = str(excel_path.absolute())

    e2d= ExcelToDb()
    df = e2d.read_excel(abs_path)
    # print(df)
    # e2d.excel_to_db(df, model)



def main():
    pass
    # station_path = './station/temp/station_c62x_20230613.xlsx'
    # station_model = station.Station
    export_path = './station/views/fields'
    guowangbuliang_path = './station/temp/guowangbuliang.xlsx'
    guowangbuliang_model = guowangbuliang.Guowangbuliang
    pbom_path = './station/temp/PBOM_116020200824（C62X 白车身）.xlsx'
    pbom_model = pbom.Pbom
    handian_path = './station/temp/weldspot/焊点清单.xlsx'
    handian_model = handian.Handian
    hanqiang_path = './station/temp/weldspot/焊枪清单.xlsx'
    cailiao_path = './station/temp/weldspot/材料清单.xlsx'
    cailiao_model = cailiao.Cailiao




    ########################
    name = 'cailiao'
    path = cailiao_path
    model =cailiao_model

    ###################################
    # 创建转换工具实例对象e2d
    e2d = ExcelToDb()

    ###################

    df = e2d.read_excel(path)
    e2d.export_col_name_map(os.path.join(export_path, f'{name}.json'))
    
    # 生成model的字段代码，复制到models.py中
    e2d.generate_model_code(os.path.join(export_path, f'{name}.demo2'))

    print(df)

    e2d.excel_to_db(df, model)
    



if __name__ == '__main__':
    main()